package com.crm.jdy.common.shiro;

import com.crm.jdy.common.utils.redis.RedisUtils;
import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;


public class TokenUtil {

    private static final String secret = "secret";
    public static final String tokenHeard = "tokenHead";
    private static final Long expTime = 60 * 60 * 1000L;

    public static String getToken(String name,String id,String ip) {
        if(ip!=null){
            ip+=secret;
        }
        JwtBuilder builder = Jwts.builder();
        builder.signWith(SignatureAlgorithm.HS256,ip);
        builder.setId(id).setSubject(name).setAudience(ip);
        builder.setIssuedAt(new Date()).setExpiration(new Date(System.currentTimeMillis() + expTime));
        String token = builder.compact();
        return token;
    }

    public static Claims getTokenBody(String token,String ip) {
        if(ip!=null){
            ip+=secret;
        }
        JwtParser parser = Jwts.parser();
        Claims body = parser.setSigningKey(ip).parseClaimsJws(token).getBody();
        return body;
    }

    public static String getName(String token,String ip) {
        Claims body = getTokenBody(token,ip);
        String id = body.getId();
        return id;
    }

}

